import './js/weapp-adapter'
import Render from './js/Render.js'
import Music from './js/Music.js'

const ctx = canvas.getContext('2d')
var render = new Render(ctx)
const music = new Music()

var id

function gameInit(){
  render.initBlocks()
  render.drawBg()
  render.listenBegin().then(() => {
      if (render.isBegin) {
        id = setInterval(gameStart, 16.67)
      }
  })
}
gameInit()

function gameStart () {
  ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
  render.drawBg()
  render.playclick()
  render.showScore()
  if(render.isOver){
    music.errPlay()
    clearInterval(id)
    isAgain()
  }
  //requestAnimationFrame(gameStart)
}
function isAgain () {
  wx.showModal({
    title: '温馨提示',
    content: 'game over!',
    showCancel: true,
    cancelText: '退出游戏',
    confirmText: '再来一局',
    success: (res) => {
      if (res.confirm) {
        render = new Render(ctx)
        gameInit()
      } else if (res.cancel) {
        
      }
    }
  })
}
//gameStart()


